{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [],
   "source": [
    "%classpath add ../../../../../../target/classes\n",
    "import designpattern.adapter.tree.*;\n",
    "import java.util.stream.Collectors;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "显示一个文件目录\n",
    "```plaintext\n",
    "├── tree\n",
    "│   ├── demo\n",
    "│   │   ├── FolderTree.java\n",
    "│   │   ├── OrgTree.java\n",
    "│   │   ├── DirTreeProvider.java\n",
    "│   │   ├── SimpleTree.java\n",
    "│   │   └── FileNameProvider.java\n",
    "│   ├── VisualTreeViewer.java\n",
    "│   ├── TreeContentProvider.java\n",
    "│   ├── README.md\n",
    "│   ├── SimpleTreeViewer.java\n",
    "│   └── NameProvider.java\n",
    "└── livedemo\n",
    "    ├── orgtree.ipynb\n",
    "    └── foldertree.ipynb\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "java中与文件目录相关的API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "llm dir\n",
      "stringeditor dir\n",
      "README.md file\n",
      "livedemo dir\n"
     ]
    }
   ],
   "source": [
    "import java.io.File;\n",
    "\n",
    "File root = new File(\"../../command\");\n",
    "Arrays.asList(root.listFiles()).forEach(f -> {\n",
    "    System.out.println(f.getName() + \" \" + (f.isDirectory() ? \"dir\" : \"file\"));\n",
    "});\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以认为，File就是领域模型，而我们希望将这个领域模型适配成树形结构。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [],
   "source": [
    "public class DirTreeProvider implements TreeContentProvider<File> {\n",
    "\n",
    "    private File root;\n",
    "\n",
    "    public DirTreeProvider(File root) {\n",
    "        this.root = root;\n",
    "    }\n",
    "\n",
    "    @Override\n",
    "    public List<File> getRoots() {\n",
    "        return Arrays.asList(root.listFiles());\n",
    "    }\n",
    "\n",
    "    @Override\n",
    "    public List<File> getChildren(File parent) {\n",
    "        if (parent.isDirectory())\n",
    "            return Arrays.asList(parent.listFiles());\n",
    "        else\n",
    "            return List.of();\n",
    "    }\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "public class FileNameProvider implements NameProvider<File> {\n",
    "\n",
    "    @Override\n",
    "    public String getName(File file) {\n",
    "        return file.getName();\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [],
   "source": [
    "TreeContentProvider<File> cp = new DirTreeProvider(\n",
    "        new File(\"../\"));\n",
    "NameProvider<File> nameProvider = new FileNameProvider();\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tree\n",
      "    demo\n",
      "        FolderTree.java\n",
      "        OrgTree.java\n",
      "        DirTreeProvider.java\n",
      "        SimpleTree.java\n",
      "        FileNameProvider.java\n",
      "    VisualTreeViewer.java\n",
      "    TreeContentProvider.java\n",
      "    README.md\n",
      "    SimpleTreeViewer.java\n",
      "    NameProvider.java\n",
      "livedemo\n",
      "    orgtree.ipynb\n",
      "    foldertree.ipynb\n"
     ]
    }
   ],
   "source": [
    "new SimpleTreeViewer<File>(cp, nameProvider).show();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "├── tree\n",
      "│   ├── demo\n",
      "│   │   ├── FolderTree.java\n",
      "│   │   ├── OrgTree.java\n",
      "│   │   ├── DirTreeProvider.java\n",
      "│   │   ├── SimpleTree.java\n",
      "│   │   └── FileNameProvider.java\n",
      "│   ├── VisualTreeViewer.java\n",
      "│   ├── TreeContentProvider.java\n",
      "│   ├── README.md\n",
      "│   ├── SimpleTreeViewer.java\n",
      "│   └── NameProvider.java\n",
      "└── livedemo\n",
      "    ├── orgtree.ipynb\n",
      "    └── foldertree.ipynb\n"
     ]
    }
   ],
   "source": [
    "new VisualTreeViewer<File>(cp, nameProvider).show();"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".jshell",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "11.0.21+9-LTS"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
